﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// Summary description for LatestFilesController
/// </summary>
public class LatestFilesController
{
    readonly List<string> _reports = new List<string>();
	public LatestFilesController()
	{
		//
		// TODO: Add constructor logic here
		//
	}

   // Error	8	Argument 1: cannot convert from 'System.Web.HttpServerUtilityBase' to 'System.Web.HttpServerUtility'	E:\LaosTraveller\ws\Views\latestfiles.cshtml	8	49	E:\LaosTraveller\ws\

    public string DoFileReport(System.Web.HttpServerUtilityBase Server)
    {
        _reports.Clear();

        _reports.Add("<div class= 'filereport'>");

        string webRoot = Server.MapPath("~/");
        var theFiles = new List<string>();
        DumpFolderNameAndFileNames(webRoot, theFiles);
        theFiles.Sort(new MyFileTimeSorter());

        foreach (var f in theFiles)
        {
            try
            {
                // <a href="maplaos.aspx?XamNeua">Xam Neua</a>

                var sb = new StringBuilder();
                sb.Append("<a href='../");
                string fromRoot = f.Substring(webRoot.Length);
                sb.Append(fromRoot);
                sb.Append("'>");
                sb.Append("" + Server.HtmlEncode(fromRoot));
                sb.Append("</a>");

                DateTime lastWriteTime = File.GetLastWriteTimeUtc(f);
                sb.Append(" ");
                sb.Append(lastWriteTime.ToShortDateString());
                sb.Append(" ");
                sb.Append(lastWriteTime.ToShortTimeString());

                sb.Append(" UTC\r\n");
                _reports.Add(sb.ToString());
            }
            catch (Exception)
            {
                _reports.Add(f);
            }
        }
        _reports.Add("</div>");

        var sb1 = new StringBuilder();
        foreach (var report in _reports)
        {
            sb1.Append(report);
            sb1.Append("<br />");
        }
        return sb1.ToString();
    }

    private static void DumpFolderNameAndFileNames(string folderPath, List<string> theFiles)
    {
        if (folderPath.ToLower().Contains("\\bin"))
            return;

        if (folderPath.EndsWith("_svn"))
            return;

        if (folderPath.EndsWith("_vti_cnf"))
            return;

        if (folderPath.EndsWith("_vti_pvt"))
            return;

        if (folderPath.EndsWith("App_Code"))
            return;

        string[] dirs = Directory.GetDirectories(folderPath);
        foreach (var d in dirs)
        {
            DumpFolderNameAndFileNames(d, theFiles);
        }

        string[] fileNames = Directory.GetFiles(folderPath);

        theFiles.AddRange(fileNames);
    }

    class MyFileTimeSorter : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            DateTime lastWriteTimeX = File.GetLastWriteTimeUtc(x);
            DateTime lastWriteTimeY = File.GetLastWriteTimeUtc(y);
            return (int)((lastWriteTimeY - lastWriteTimeX).TotalSeconds);
        }
    }
}